package com.example.springboot.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.*;
import com.example.springboot.controller.dto.UserDTO;
import com.example.springboot.controller.dto.UserPasswordDTO;
import com.example.springboot.entity.Validation;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.service.IValidationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import com.example.springboot.service.IUserService;
import com.example.springboot.entity.User;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgc
 * @since 2022-12-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @Resource
    private IValidationService validationService;

    //登录
    @HoneyLogs(operation = "用户",type = LogType.LOGIN)
    @PostMapping("/loginAccount")
    public Result loginAccount(@RequestBody UserDTO userDTO) {  //@RequestBody接收前端Json转成后台java对象
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        //校验是否为空
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        UserDTO dto = userService.login(userDTO);
        return Result.success(dto);
    }

    @HoneyLogs(operation = "用户",type = LogType.LOGIN)
    @AuthAccess
    @PostMapping("/loginEmail")
    public Result loginEmail(@RequestBody UserDTO userDTO) {  //@RequestBody接收前端Json转成后台java对象
        String email = userDTO.getEmail();
        String code = userDTO.getCode();
        //校验是否为空
        if (StrUtil.isBlank(email) || StrUtil.isBlank(code)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        UserDTO dto = userService.loginEmail(userDTO);
        return Result.success(dto);
    }

    //注册
    @HoneyLogs(operation = "用户",type = LogType.REGISTER)
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO) {  //@RequestBody接收前端Json转成后台java对象
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();

        //校验是否为空
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_600, "参数错误");
        }
        return Result.success(userService.register(userDTO));
    }

    /**
     * 修改密码
     *
     * @param userPasswordDTO
     * @return
     */
    @HoneyLogs(operation = "用户",type = LogType.UPDATE)
    @PostMapping("/password")
    public Result password(@RequestBody UserPasswordDTO userPasswordDTO) {
//        userPasswordDTO.setPassword(SecureUtil.md5(userPasswordDTO.getPassword()));
//        userPasswordDTO.setNewPassword(SecureUtil.md5(userPasswordDTO.getNewPassword()));
        userService.updatePassword(userPasswordDTO);
        return Result.success();
    }

    @AuthAccess
    @PutMapping("/reset")
    public Result reset(@RequestBody UserPasswordDTO userPasswordDTO){
        if(StrUtil.isBlank(userPasswordDTO.getEmail())|| StrUtil.isBlank(userPasswordDTO.getCode())){
            throw new ServiceException("-1","参数异常");
        }
        // 先查询邮箱验证的表，看看之前有没有发送过邮箱code, 如果不存在，就重新获取
        QueryWrapper<Validation> validationQueryWrapper = new QueryWrapper<>();
        validationQueryWrapper.eq("email", userPasswordDTO.getEmail());
//        大于等于当前时间
        validationQueryWrapper.ge("time", new Date());
        Validation one = validationService.getOne(validationQueryWrapper);
        if (one == null) {
            throw new ServiceException("-1", "验证码过期，请重新获取");
        }
        validationQueryWrapper.eq("code", userPasswordDTO.getCode());
        Validation code = validationService.getOne(validationQueryWrapper);
        if (code == null) {
            throw new ServiceException("-1", "验证码错误");
        }
        // 如果验证通过了就查询用户的信息
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("email", userPasswordDTO.getEmail()); //存根据邮箱email查询用户信息
        User user = userService.getOne(userQueryWrapper);
        // 重置密码
        user.setPassword("123");
        userService.updateById(user);
        return Result.success();
    }
    //新增和修改
    @HoneyLogs(operation = "用户",type = LogType.UPDATE)
    @PostMapping
    public Result save(@RequestBody User user) {  //@RequestBody接收前端Json转成后台java对象
        //新增或者更新
        return Result.success(userService.saveOrUpdate(user));
    }

    @AuthAccess // 绕过token
    @GetMapping("/email/{email}/{type}")
    public Result sendEmailCode(@PathVariable String email, @PathVariable Integer type) throws MessagingException {
        if (StrUtil.isBlank(email)) {
            throw new ServiceException(Constants.CODE_400, "参数错误");
        }
        if (type == null) {
            throw new ServiceException(Constants.CODE_400, "参数错误");
        }
        userService.sendEmailCode(email, type);
        return Result.success();
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(userService.list());
    }

    @GetMapping("/role/{role}")
    public Result findUsersByRole(@PathVariable String role) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role", role);
        List<User> list = userService.list(queryWrapper);
        return Result.success(list);
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(userService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(userService.removeByIds(ids));
    }

    //通过id查找
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }

    @GetMapping("/username/{username}")
    public Result findOne(@PathVariable String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return Result.success(userService.getOne(queryWrapper));
    }


    /**
     * //分页查询Mybatis-plus的方式
     *
     * @GetMapping("/page") public Result findPage(@RequestParam Integer pageNum,
     * @RequestParam Integer pageSize,
     * @RequestParam(defaultValue = "") String username,
     * @RequestParam(defaultValue = "") String email,
     * @RequestParam(defaultValue = "") String address) {
     * <p>
     * QueryWrapper<User> queryWrapper = new QueryWrapper<>();
     * queryWrapper.orderByDesc("id");
     * if(!"".equals(username)){
     * queryWrapper.like("username",username);
     * }
     * if(!"".equals(email)){
     * queryWrapper.like("email",email);
     * }
     * if(!"".equals(address)){
     * queryWrapper.like("address",address);
     * }
     * //        //获取当前用户信息
     * User currentUser = TokenUtils.getCurrentUser();
     * System.out.println("当前用户信息=============" + currentUser.getNickname());
     * <p>
     * return Result.success(userService.page(new Page<>(pageNum, pageSize), queryWrapper));
     * }
     */

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String username,
                           @RequestParam(defaultValue = "") String email,
                           @RequestParam(defaultValue = "") String address) {

//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        if(!"".equals(username)){
//            queryWrapper.like("username",username);
//        }
//        if(!"".equals(email)){
//            queryWrapper.like("email",email);
//        }
//        if(!"".equals(address)){
//            queryWrapper.like("address",address);
//        }
//        //获取当前用户信息
//        User currentUser = TokenUtils.getCurrentUser();
//        System.out.println("当前用户信息=============" + currentUser.getNickname());

        return Result.success(userService.findPage(new Page<>(pageNum, pageSize), username, email, address));
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<User> list = userService.list();
        //通过工具writer 写出到磁盘路径
        //   ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");

        //在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);

        //给表格数据指定别名
        writer.addHeaderAlias("username", "用户名");
        writer.addHeaderAlias("password", "密码");
        writer.addHeaderAlias("nickname", "昵称");
        writer.addHeaderAlias("email", "邮箱");
        writer.addHeaderAlias("phone", "电话");
        writer.addHeaderAlias("address", "地址");
        writer.addHeaderAlias("createTime", "创建时间");
        writer.addHeaderAlias("avatarUrl", "头像");

        //一次性写出list内的对象到excel,使用默认样式，强制输出标题
        writer.write(list, true);

        //设置浏览器响应的样式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }

    /**
     * excel 导入
     *
     * @param file
     * @throws Exception
     */

    @PostMapping("/import")
    public Result imp(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        // 通过 javabean的方式读取Excel内的对象，但是要求表头必须是英文，跟javabean的属性对应起来
//        List<User> list = reader.readAll(User.class);

        //忽略表头的中文，直接读取表的内容
        List<List<Object>> list = reader.read(1);
        List<User> users = CollUtil.newArrayList();
        for (List<Object> row : list) {
            User user = new User();
            user.setUsername(row.get(0).toString());
            user.setPassword(row.get(1).toString());
            user.setNickname(row.get(2).toString());
            user.setEmail(row.get(3).toString());
            user.setPhone(row.get(4).toString());
            user.setAddress(row.get(5).toString());
            user.setAvatarUrl(row.get(6).toString());
            users.add(user);

        }
//        批量插入
        userService.saveBatch(users);
        return Result.success(true);
    }
}



